<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="shortcut icon" href="/logo.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="__CSS__/font.css">
    <link rel="stylesheet" href="__CSS__/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="__STATIC__/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="__JS__/xadmin.js"></script>
    {include file="layout/iview" /}
</head>
<body>
<form class="layui-form" action="" style="margin-top:20px">
    <div class="layui-form-item">
        <label class="layui-form-label">游戏名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入标题"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">游戏大类</label>
        <div class="layui-input-block">
            <select name="major_type">
                <option value="1">网游</option>
                <option value="2">小游戏</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">游戏gid</label>
        <div class="layui-input-block">
            <input type="text" name="gid" lay-verify="required" placeholder="请输入" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">游戏图标</label>
        <div class="layui-input-block">
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="test1">上传图片</button>
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="demo1" style="width:100px;height: 100px">
                    <p id="demoText"></p>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">游戏分类</label>
        <div class="layui-input-block">
            <select name="type">
                {volist name="type" id="v"}
                <option value="{$v.id}">{$v.name}</option>
                {/volist}
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">游戏标签</label>
        <div class="layui-input-block">
            {volist name="tag" id="v"}
            <input type="checkbox" name="tag[]" value="{$v.id}" title="{$v.name}">
            {/volist}
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">游戏排序id</label>
        <div class="layui-input-block">
            <input type="number" name="order_id" lay-verify="required" placeholder="排序id(数值越大，排序越前,默认为0)" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">游戏描述</label>
        <div class="layui-input-block">
            <textarea name="des" lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">提交</button>
        </div>
    </div>

</form>
<script>
    var Global = {};
    layui.use('form', function () {
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function (data) {
            var upload = data.field;
            upload.img = Global.img;
            $.post('/crm/game/gameaddact', upload, function (res) {
                layer.alert(res.msg);
            }, 'json');
            return false;
        });
    });


    layui.use('upload', function () {
        var $ = layui.jquery
            , upload = layui.upload;
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/crm/game/uploadImg'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                Global.img = res.data.src;
                //上传成功
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

    });
</script>
</body>
</html>